//
//  HJAskToBuyViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJAskToBuyViewController.h"
#import "HJAskCell.h"
#import "HJOderDetailController.h"
#import "HJSellOderDetailController.h"
#import "HJMineModel.h"
#import "HJBuyCell.h"
#import "HJPayViewController.h"
#import "HJLogisticsViewController.h"// 物流


@interface HJAskToBuyViewController ()
@property(nonatomic,strong)UIView *line;
@property(nonatomic,assign)NSInteger tag;
@property(nonatomic,strong) UIView * redView;
@property(nonatomic,strong) NSMutableArray <HJSellModel *>* dataArr;
@property(nonatomic,strong) NSMutableArray <HJSellModel *>* buyArr;
@end

@implementation HJAskToBuyViewController
-(NSMutableArray<HJSellModel *> *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
-(NSMutableArray<HJSellModel *> *)buyArr {
    if (!_buyArr) {
        _buyArr = [NSMutableArray array];
    }
    return _buyArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configHeader];
    self.tag = 10;
    [mNotificationCenter addObserver:self selector:@selector(PayBtnNotification:) name:@"PayBtnNotification" object:nil];
    [mNotificationCenter addObserver:self selector:@selector(logisticsNotification:) name:@"logisticsNotification" object:nil];
    [self animationLineWithBtn:[self.view viewWithTag:self.num_tag.integerValue]];
    [self Refresh];
    if ([self.num_tag isEqualToString:@"10"]) {
        [self loadNetworkData];
    }else {
        [self loadBuyWorkData];
    }
}
- (void)Refresh {
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithHeader)];
        self.tableView.mj_header = header;

    if (self.needRefresh) {
        MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithFooter)];
        self.tableView.mj_footer = footer;
    }
}

- (void)refreshDataWithFooter {
    self.page ++;
    if (self.page > [self.totalPage integerValue]) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } else {
        if ([self.num_tag isEqualToString:@"10"]) {
            [self loadNetworkData];
        }else {
            [self loadBuyWorkData];
        }
    }
}

- (void)refreshDataWithHeader {
    self.page = 1;
    if ([self.num_tag isEqualToString:@"10"]) {
        [self loadNetworkData];
    }else {
        [self loadBuyWorkData];
    }
}
-(void)configTableView {
    self.needRefresh = NO;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    CGFloat H;
    if (mScreenHeight<812) {
        H = mScreenHeight -64-40;
    }else {
        H = mScreenHeight -TabBar_H -34-40;
    }
    self.tableView.frame = CGRectMake(0, 40, mScreenWidth, H);
    [self.tableView registerCellByNibName:@"HJAskCell"];
    
}

-(void)configHeader {
    UIView * header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mScreenWidth, 40)];
    [self.view addSubview:header];
    NSString * title_B;
    if ([self.num_tag isEqualToString:@"10"]) {
        title_B = @"PingFangSC-Semibold";
    }else {
        title_B = @"PingFangSC-Regular";
    }
    UIButton * btn1 = [self configWithButton:@"求购" selecteTitle:nil titleColor:HJColorRGB(0) selecteTitleColor:nil frame:CGRectMake(15, 10, 40, 20) backGroundImage:nil image:nil selecteImage:nil font:[UIFont fontWithName:title_B size:15]];
    btn1.alpha = 1;
    btn1.tag = 10;
    [btn1 addTarget:self action:@selector(buy:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * btn2 = [self configWithButton:@"购买" selecteTitle:nil titleColor:HJColorRGB(0) selecteTitleColor:nil frame:CGRectMake(85, 10, 40, 20) backGroundImage:nil image:nil selecteImage:nil font:[UIFont fontWithName:title_B size:15]];
    btn2.alpha = 0.5;
    btn2.tag = 11;
    [btn2 addTarget:self action:@selector(buy:) forControlEvents:UIControlEventTouchUpInside];
    
    _redView = [[UIView alloc]initWithFrame:CGRectMake(120, 7, 10, 10)];
    _redView.backgroundColor= HJColor(255, 72, 74);
    _redView.layer.cornerRadius = 5;
    _redView.layer.masksToBounds = YES;
    [header addSubview:_redView];
    
    _line =[[UIView alloc]initWithFrame:CGRectMake(btn1.x, 39, btn1.width, 1)];
    _line.backgroundColor = HJColorRGB(0);
    
    [header addSubview:btn1];
    [header addSubview:btn2];
    [header addSubview:_line];
}

-(void)buy:(UIButton *)btn {
    [self animationLineWithBtn:btn];
    self.page=1;
    if (btn.tag ==10) {
        [self.dataArr removeAllObjects];
        [self loadNetworkData];
        self.tag =10;
    }else {
        [self.buyArr removeAllObjects];
        [self loadBuyWorkData];
        self.tag =11;
    }
    [self.tableView reloadData];
}
// 滑动线条
-(void)animationLineWithBtn:(UIButton *)btn {
    for (int i = 10; i<=11; i ++) {
        UIView *view = [self.view viewWithTag:i];
        view.alpha = 0.5;
        btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
    }
    btn.alpha = 1;
    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
    [UIView animateWithDuration:0.1 animations:^{
        _line.centerX = btn.centerX;
    }];
}
-(void)loadNetworkData {
    [TZHttpTool post:WantToBuyAPI params:@{@"page":@(self.page)} success:^(NSDictionary *result) {
        NSArray * arr = [HJSellModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"models"]];
        if (self.page==1) {
            [self.dataArr removeAllObjects];
        }
        self.totalPage = result[@"data"][@"totalPage"];
        [self.dataArr addObjectsFromArray:arr];
        if ([result[@"data"][@"want_buy"] isEqual:@0]) {
            _redView.hidden = YES;
        }else {
            _redView.hidden = NO;
        }
        [self.tableView reloadData];
        [self.tableView endRefresh];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}

-(void)loadBuyWorkData {
    [TZHttpTool post:BuyAPI params:@{@"page":@(self.page)} success:^(NSDictionary *result) {
        NSArray * arr =[HJSellModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"models"]];
        if (self.page==1) {
            [self.buyArr removeAllObjects];
        }
        self.totalPage = result[@"data"][@"totalPage"];
        [self.buyArr addObjectsFromArray:arr];
        if ([result[@"data"][@"buy"] isEqual:@0]) {
            _redView.hidden = YES;
        }else {
            _redView.hidden = NO;
        }
        [self.tableView endRefresh];
        [self.tableView reloadData];
    } failure:^(NSString *msg) {
        [self showSuccessHUDWithStr:msg];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.tag==10) {
        if (self.dataArr.count>0) {
            [tableView hideNoData];
            return self.dataArr.count;
        }
    }else {
        if (self.buyArr.count>0) {
            [tableView hideNoData];
            return self.buyArr.count;
        }
    }
    [tableView showNoData];
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJAskCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HJAskCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.tag==10) {
        cell.sellModel = self.dataArr[indexPath.row];
        return cell;
    }else {
        HJBuyCell * buyCell = [tableView dequeueReusableCellWithIdentifier:@"HJBuyCell"];
        if (!buyCell) {
            buyCell = [[NSBundle mainBundle]loadNibNamed:@"HJBuyCell" owner:self options:nil].firstObject;
        }
        buyCell.selectionStyle = UITableViewCellSelectionStyleNone;
        buyCell.buyModel = self.buyArr[indexPath.row];
        
        return buyCell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.tag==10) {
        HJSellModel * model = self.dataArr[indexPath.row];
        return model.cellHeight;
    }else {
        HJSellModel * model = self.buyArr[indexPath.row];
        return model.cellHeight;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJOderDetailController * oderDetailVC = [[HJOderDetailController alloc]init];
    oderDetailVC.navTitle= @"订单详情";
    if (self.tag==10) {
        HJSellModel * model = self.dataArr[indexPath.row];
        oderDetailVC.type= @1;
        oderDetailVC.want_buy_id = model.want_buy_id;
    }else {
        HJSellModel * model = self.buyArr[indexPath.row];
        oderDetailVC.type = @2;
        oderDetailVC.order_id = model.order_id;
    }
    [self.navigationController pushViewController:oderDetailVC animated:YES];
}
#pragma  mark -- 支付
-(void)PayBtnNotification:(NSNotification *)not {
    HJPayViewController * payVC = [[HJPayViewController alloc]init];
    payVC.pay = not.userInfo[@"percent"];
    payVC.order_type = @"4";
    payVC.order_sn = not.userInfo[@"order_sn"];
    [self.navigationController pushViewController:payVC animated:YES];
}
#pragma mark --  物流详情
-(void)logisticsNotification:(NSNotification *)not {
    
    if ([not.userInfo[@"express"] isEqualToString:@""]) {
        [self showSuccessHUDWithStr:@"暂未发货"];
        return;
    }
    HJLogisticsViewController * logisticsVC = [[HJLogisticsViewController alloc]init];
    logisticsVC.shoes_name = not.userInfo[@"shoes_name"];
    logisticsVC.shoes_size = not.userInfo[@"shoes_size"];
    logisticsVC.shoes_money = not.userInfo[@"shoes_money"];
    logisticsVC.shoes_image = not.userInfo[@"shoes_image"];
    logisticsVC.express = not.userInfo[@"express"];
    [self.navigationController pushViewController:logisticsVC animated:YES];
}

@end
